﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deferred.Library.Scene.Visual
{
    class CameraVisual : GraphicalComponent
    {
        private float viewWidth = 0;
        private float viewHeight = 0;

        public Camera Camera { get; private set; }

        public BoundingFrustum Frustum { get { return new BoundingFrustum(View * Projection); } }
        public Vector3 Position { get { return Vector3.Transform(Vector3.Zero, Matrix.Invert(View)); } }
        public Matrix View
        {
            get
            {
                return Matrix.CreateTranslation(0, -10, 0) *
                       Matrix.CreateRotationY(MathHelper.ToRadians(Camera.CameraRotation)) *
                       Matrix.CreateRotationX(MathHelper.ToRadians(Camera.CameraArc)) *
                       Matrix.CreateLookAt(new Vector3(0, 0, -Camera.CameraDistance), new Vector3(0, 0, 0), Vector3.Up);
            }
        }
        public Matrix Projection
        {
            get
            {
                float aspectRatio = (float)viewWidth / (float)viewHeight;
                return Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1, 3000);
            }
        }

        public CameraVisual(IGraphicsDeviceService deviceService, Camera camera)
            : base(deviceService)
        {
            this.Camera = camera;
        }

        protected override void LoadContent()
        {
            viewWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
            viewHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;
        }
    }
}
